package online.shenjian.mq;

import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@ConditionalOnProperty(name = "spring.rabbitmq.enable", matchIfMissing = true)
public class HeaderSender {

    private RabbitMessagingTemplate rabbitMessagingTemplate;

    public HeaderSender(RabbitMessagingTemplate rabbitMessagingTemplate) {
        this.rabbitMessagingTemplate = rabbitMessagingTemplate;
    }

    public void send() {
        String msg = "Hello World!";
        Map<String, Object> headerMap = new HashMap<>();
        headerMap.put("h1", "Header1");
        headerMap.put("h3", "Header3");
        rabbitMessagingTemplate.convertAndSend("headers", "", msg, headerMap);

        msg = "My Girl!";
        headerMap.clear();
        headerMap.put("h1", "Header1");
        headerMap.put("h2", "Header2");
        rabbitMessagingTemplate.convertAndSend("headers", "", msg, headerMap);
    }
}
